
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//FILE* stdin;
//FILE* stderr;
//FILE* stdout;

FILE* inputFile;

void imprimirAyuda(){
	fprintf(stdout, "U\ntp0 [OPTIONS] [File ...]\n");
	fprintf(stdout, "    -h,  --help           display this help and exit.\n");
	fprintf(stdout, "    -v,  --version        display version information and exit.\n");
	fprintf(stdout, "    -m,  --merge          use the mergesort algorithm.\n");
	fprintf(stdout, "    -s,  --sel            use the selectionsort algorithm.\n\n");
}

void imprimirVersion(){
	fprintf(stdout, "Version 0.1 \n \n");
}

char* append(char* to, char c){
	int last = strlen(to);
	to[last] = c;
	to[last + 1] = '\0';
	return to;
}

void merge(char* left, char* right, char* result){
	 int lenLeft = strlen(left);
	 int lenRight = strlen(right);
     while ( lenLeft > 0 || lenRight > 0 ){
        if (lenLeft > 0 && lenRight > 0){
			if ( left[0] <= right[0] ){
				result = append(result, left[0]);
				left++;
            }else{
 				result = append(result, right[0]);
 				right++;
            }
        }else if ( lenLeft > 0 ){
 				result = append(result, left[0]);
  				left++;
        }else if ( lenRight > 0 ){
                result = append(result, right[0]);
				right++;
        }
        lenLeft = strlen(left);
	 	lenRight = strlen(right);
    }

}    


void merge_sort(char* m , char* resultado){
	 int len = strlen(m);
    if( len <= 1 ){
        resultado[0] = m[0];
    }else{
		 int middle = (len / 2);
		 char left[middle+1];
		 left[0] = '\0';
		 char right[middle+1];
		 right[0] = '\0';
		 int x;
		 for(x = 0; x < middle; x++){
		   left[x] = m[x];
		   left[x + 1] = '\0';
		 }
		 int i;
		 int rig = 0;
 		 for( i = middle; i < len; i++ ){
			right[rig] = m[i];
			right[rig + 1] = '\0';
			rig++;
		 } 
		char resultadoLeft[strlen(left)];
		resultadoLeft[0] = '\0';
		merge_sort(left, resultadoLeft);
		char resultadoRight[strlen(right)];
		resultadoRight[0] = '\0';
		merge_sort(right, resultadoRight);

		merge(resultadoLeft, resultadoRight, resultado);
    }
}

int main(int argc, char* argv[]) {

	if (argc == 1){
		fprintf(stderr, "Se debe indicar una opcion.\nIngrese -h para obtener ayuda.\n");
		return 1;
	}

	int comandoInvalido = 1; // Para saber si no se ejecuta ninguna opcion

	int i;
	for(i = 1; i < argc; i++ ){

		// merge
		if ((argv[i][0] == '-') && (argv[i][1] == 'm')){
			printf("funcion merge \n");
			//char* cadena = "9876543210";
			
			char linea[100];
			int lee_stdin = 0;
			long elapsed_seconds;
			// while( fgets(linea, 100, stdin) ){
				// printf("linea %s \n", linea);
				// lee_stdin = 1;
			// }
			
			if(lee_stdin == 0){
				printf("lee de archivos \n");
				
				size_t message_len = 1;
				char *message = (char*) malloc(message_len);
				strncat(message, "\0", message_len);

				
				int k;
				for(k = i+1; k < argc; k++ ){
					printf("Archivo: %s \n" , argv[k]);
					inputFile = fopen (argv[k], "rt");
					while(fgets(linea, 100, inputFile) != NULL){
						sscanf (linea, "%ld", &elapsed_seconds);
						printf ("lei: %s \n", linea);
						 
						message_len += strlen(linea);
						message = (char*) realloc(message, message_len);
						strncat(message, linea, message_len);
						 
					}
					fclose(inputFile);
				}
				printf ("concat: %s \n", message);
				puts(message);
				free(message);
				
				//linea[0] = '\0';
			}
			
			//char resultado[strlen(linea)];
			//resultado[0] = '\0';
			
			//merge_sort(linea, resultado);
			//printf("Cadena ordenada: %s \n", resultado);
			comandoInvalido = 0;
		}
		
		// merge
		if ((argv[i][0] == '-') && (argv[i][1] == 'v')){
			imprimirVersion();
			comandoInvalido = 0;
		}

		// Ayuda
		if (((argv[i][0] == '-') && (argv[i][1] == 'h')) || ((argv[i][0] == '-') && (argv[i][1] == '-') && (argv[i][2] == 'h') && (argv[i][3] == 'e') && (argv[i][4] == 'l') && (argv[i][5] == 'p') )){
			imprimirAyuda();
			comandoInvalido = 0;
		}

		// Version
		if (((argv[i][0] == '-') && (argv[i][1] == 'V')) || ((argv[i][0] == '-') && (argv[i][1] == '-') && (argv[i][2] == 'v') && (argv[i][3] == 'e') && (argv[i][4] == 'r') && (argv[i][5] == 's') && (argv[i][6] == 'i') && (argv[i][7] == 'o') && (argv[i][8] == 'n') )){
			imprimirVersion();
			comandoInvalido = 0;
		}
		
		

	}

	if (comandoInvalido){
		fprintf(stderr, "Se esperaba una opcion. \nIngrese -h para obtener ayuda.\n");
	}

	return 0;

}

